﻿@using Lavka.Core
@using Lavka.Models
@model Lavka.Models.Orders

@{
    ViewBag.Title = "Оформление заказа";
}

<h2>Оформление заказа</h2>

@using (Html.BeginForm()) 
{
    @Html.ValidationSummary(true)
    <table style="border-spacing: 5px;">
    <tr>
        <td>
        <div class="panel">
            <div class="header">Товары</div> 
            @if (CartHelper.Cart.Count > 0)
            {
            <div>
                <table class="order-table">
                <tr>
                    <th>Название товара</th>
                    <th>Кол-во</th>
                    <th>Цена</th>
                    <th>Удалить</th>
                </tr>
                    @foreach (var product in CartHelper.Cart)
                    {
                        <tr>
                            <td>
                                @product.Name
                            </td>
                            <td>
                                @{
                                    string numId = "num_" + product.ProductId;
                                }
                                @Html.TextBox(numId, "1", new { @class="text", style="width: 30px;" })
                            </td>
                            <td>
                                @{
                                    string priceId = "price_" + product.ProductId;
                                }
                                <div id="@priceId"> @ProductsHelper.GetProductPrice(product, new LavkaDbEntities()) грн.</div>
                            </td>
                            <td>
                                <a href="/Orders/DeleteItem/@product.ProductId">
                                <img src="@Url.Content("~/Content/images/delete.png")"/>
                                </a>
                            </td>
                        </tr>
                    }
                <tr>
                    <td colspan="4">
                        <hr/>
                    </td>                
                </tr>
                <tr>
                    <td>
                        <input id="btnRecalculate" type="button" value="Пересчитать" onclick="UpdatePrice()" class="text" />
                    </td>
                    <td>
                        <div style="text-align: right">
                            <b style="padding: 5px">всего:</b>
                        </div>                       
                    </td> 
                    <td>
                       <div id="SumPrice">
                       @CartHelper.GetSumPrice(new LavkaDbEntities()) грн.
                       </div>
                    </td>                                    
                </tr>                

                </table>
            </div>
            }
            <div>
                @Html.EditorFor(model => model.Pickup) Самовывоз
                @Html.ValidationMessageFor(model => model.Pickup)
            </div>

            <div>
                Дополнительная информация
            </div>
            <div>
                @Html.EditorFor(model => model.Description)
                @Html.ValidationMessageFor(model => model.Description)
            </div>
        </div>    
        </td>
        <td>
            <div class="panel">
                <div class="header">Данные пользователя</div> 
                     
                    <div style="padding-top: 5px; padding-bottom: 5px;">
                        <input id="rNewCustomer" type="radio" value="New" name="gUser" onclick="ChangeUserType(true)" checked="checked" />
                            Я новый покупатель
                    </div>

                    <div id="CustomerDetails" style="padding-bottom: 5px;">
                        @Html.Partial("CustomerDetails")
                    </div>
                              
                    <div  style="padding-top: 5px;padding-bottom: 5px;">

                    <input id="rExistingCustomer" type="radio" value="Existing" name="gUser" onclick="ChangeUserType(true)" />
                        У меня есть код покупки

                    </div>

                    <div id="ExistingCustomer">
                        <input id="tbCustomerKey" type="text" class="text" />
                        <input id="btnCustomerKey" type="button" onclick="GetCustomerDetailsByKey()" value="Ввести" class="button" />
                    </div>
                
            </div>
        </td>

    </tr>
    </table>
            <div class="navigateButton">
                <input id="SubmitOrder" type="submit" value="Оформить" class="button" /> |
                @Html.ActionLink("На главную", "Index", "Home")
            </div>      
}

<script type="text/javascript">
    $(document).ready(function () {
        $("#rNewCustomer").click();
        $(".panel").corner();

        $("#SubmitOrder").click(function () {
            var selectedValue = $("input[name='gUser']:checked").val();
            if (selectedValue != 'New') {
                $("#rNewCustomer").click();
                ChangeUserType(false);
            }
        });
    });
</script>